package com.fzc.rhinooffice.common.utils;

import android.annotation.SuppressLint;
import android.util.JsonReader;

import com.fzc.rhinooffice.module.entity.AttachmentRecord;
import com.fzc.rhinooffice.module.entity.Business;
import com.fzc.rhinooffice.module.entity.Email;
import com.fzc.rhinooffice.module.entity.Flow;
import com.fzc.rhinooffice.module.entity.News;
import com.fzc.rhinooffice.module.entity.Notify;
import com.fzc.rhinooffice.module.entity.Sms;
import com.fzc.rhinooffice.module.entity.User;
import com.fzc.rhinooffice.module.entity.VersionInfo;
import com.lidroid.xutils.util.LogUtils;

import org.json.JSONArray;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

@SuppressLint("NewApi")
public class JsonUtil {

    public static User analysis_user(JsonReader reader) {
        User user = new User();
        try {
            reader.beginObject();
            while (reader.hasNext()) {
                String key = reader.nextName();
                if ("ver_time".equals(key)) {
                    user.ver_time = reader.nextString();
                } else if ("ver".equals(key)) {
                    user.ver = reader.nextString();
                } else if ("ver_url".equals(key)) {
                    user.ver_url = reader.nextString();
                } else if ("mallurl".equals(key)) {
                    user.mallurl = reader.nextString();
                } else if ("userid".equals(key)) {
                    user.userid = reader.nextString();
                } else if ("a_user_name".equals(key)) {
                    user.a_user_name = reader.nextString();
                } else if ("a_sessid".equals(key)) {
                    user.a_sessid = reader.nextString();
                } else if ("a_menu_str".equals(key)) {
                    List<String> list = new ArrayList<String>();
                    String a_menu_str = reader.nextString();
                    if (!StringUtil.isEmpty(a_menu_str)) {
                        String[] arr = a_menu_str.split(",");
                        for (String s : arr) {
                            list.add(s);
                        }
                    }
                    user.a_menu_str = list;
                    list = null;
                }
            }
            reader.endObject();
        } catch (Exception e) {
            e.printStackTrace();
            return user;
        }

        return user;
    }

    // 解析邮件列表
    public static List<Sms> analysis_smss(JsonReader reader) {
        List<Sms> smsList = new ArrayList<Sms>();
        try {
            reader.beginArray();
            while (reader.hasNext()) {
                Sms sms = analysis_sms(reader);
                smsList.add(sms);
            }
            reader.endArray();
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.e(e.toString());
        } finally {
            return smsList;
        }

    }

    private static Sms analysis_sms(JsonReader reader) {
        Sms sms = new Sms();

        try {
            reader.beginObject();
            while (reader.hasNext()) {
                String key = reader.nextName();

                if ("from_id".equals(key)) {
                    sms.from_id = reader.nextString();
                }
                if ("from_name".equals(key)) {
                    sms.from_name = reader.nextString();
                }

                if ("content".equals(key)) {
                    sms.content = reader.nextString();
                }
                if ("send_time".equals(key)) {
                    sms.send_time = reader.nextString();
                }

                if ("is_new".equals(key)) {
                    sms.is_new = reader.nextString();
                }

                if ("type_name".equals(key)) {
                    sms.type_name = reader.nextString();
                }


            }
            reader.endObject();

        } catch (Exception e) {

            reader.endObject();//结束
            e.printStackTrace();
            LogUtils.e(e.toString());
        } finally {

            return sms;
        }

    }

    // 解析邮件列表
    public static List<Email> analysis_emails(JsonReader reader) {
        List<Email> emailList = new ArrayList<Email>();
        try {
            reader.beginArray();
            while (reader.hasNext()) {
                Email email = analysis_email(reader);
                emailList.add(email);
            }
            reader.endArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return emailList;
        }

    }


    // 解析邮件信息
    public static Email analysis_email(JsonReader reader) {

        Email email = new Email();

        try {
            reader.beginObject();
            while (reader.hasNext()) {
                String key = reader.nextName();

                if ("email_sl".equals(key)) {
                    email.email_sl = reader.nextString();
                }
                if ("email_id".equals(key)) {
                    email.email_id = reader.nextString();
                }
                if ("from_id".equals(key)) {
                    email.from_id = reader.nextString();
                }
                if ("from_name".equals(key)) {
                    email.from_name = reader.nextString();
                }
                if ("subject".equals(key)) {
                    email.subject = reader.nextString();
                }
                if ("content".equals(key)) {
                    email.content = reader.nextString();
                }
                if ("send_time".equals(key)) {
                    email.send_time = reader.nextString();
                }
                if ("attachment_rows".equals(key)) {
                    email.attachment_rows = reader.nextString();
                }
                if ("is_new".equals(key)) {
                    email.is_new = reader.nextString();
                }
                if ("attachment_record".equals(key)) {

                    List<AttachmentRecord> attach_list = new ArrayList<AttachmentRecord>();

                    try {
                        String fujian = reader.nextString();
                        LogUtils.e("没有附件");
                        email.attach_list = null;
                    } catch (Exception e) {
                        e.printStackTrace();
                        LogUtils.e("教训：类型一定要匹配");

                        reader.beginArray();
                        while (reader.hasNext()) {

                            AttachmentRecord attach = new AttachmentRecord();
                            reader.beginObject();
                            while (reader.hasNext()) {

                                String key2 = reader.nextName();
                                if ("url".equals(key2)) {
                                    attach.url = reader.nextString();
                                }
                                if ("name".equals(key2)) {
                                    attach.name = reader.nextString();
                                }
                            }
                            attach_list.add(attach);
                            reader.endObject();
                        }
                        reader.endArray();
                        email.attach_list = attach_list;

                    }


                }

            }
            reader.endObject();

        } catch (Exception e) {

            reader.endObject();//结束
            e.printStackTrace();
        } finally {

            return email;
        }

    }

    // 通知信息
    public static Notify analysis_notify(JsonReader reader) {
        Notify notify = new Notify();
        try {
            reader.beginObject();
            while (reader.hasNext()) {
                String key = reader.nextName();
                if ("notify_sl".equals(key)) {
                    notify.notify_sl = reader.nextString();
                } else if ("notify_id".equals(key)) {
                    notify.notify_id = reader.nextString();
                } else if ("subject".equals(key)) {
                    notify.subject = reader.nextString();
                } else if ("begin_date".equals(key)) {
                    notify.begin_date = reader.nextString();
                } else if ("type_name".equals(key)) {
                    notify.type_name = reader.nextString();
                } else if ("from_name".equals(key)) {
                    notify.from_name = reader.nextString();
                } else if ("from_id".equals(key)) {
                    notify.from_id = reader.nextString();
                } else if ("is_new".equals(key)) {
                    notify.is_new = reader.nextString();
                } else if ("content".equals(key)) {
                    notify.content = reader.nextString();
                }
            }
            reader.endObject();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return notify;
    }

    // 通知列表
    @SuppressWarnings("finally")
    public static List<Notify> analysis_notifys(JsonReader reader) {
        List<Notify> notifyList = new ArrayList<Notify>();
        try {
            reader.beginArray();
            while (reader.hasNext()) {
                Notify notify = analysis_notify(reader);
                notifyList.add(notify);
            }
            reader.endArray();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return notifyList;
        }

    }

    //新闻列表
    public static List<News> analysis_newses(JsonReader reader) {
        List<News> newsList = new ArrayList<News>();
        try {
            reader.beginArray();
            while (reader.hasNext()) {
                News news = analysis_news(reader);
                newsList.add(news);
            }
            reader.endArray();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return newsList;
        }

    }

    //新闻
    public static News analysis_news(JsonReader reader) {
        News news = new News();
        
        try {
            reader.beginObject();
            while (reader.hasNext()) {
                String key = reader.nextName();
                if ("news_sl".equals(key)) {
                    news.news_sl = reader.nextString();
                } else if ("news_id".equals(key)) {
                    news.news_id = reader.nextString();
                } else if ("subject".equals(key)) {
                    news.subject = reader.nextString();
                } else if ("news_time".equals(key)) {
                    news.news_time = reader.nextString();
                } else if ("from_name".equals(key)) {
                    news.from_name = reader.nextString();
                } else if ("type_name".equals(key)) {
                    news.type_name = reader.nextString();
                } else if ("content".equals(key)) {
                    news.content = reader.nextString();
                }
            }
            reader.endObject();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return news;
    }

    // 解析工作流类
    public static Flow analysis_flow(JsonReader reader) {
        Flow flow = new Flow();
        try {
            reader.beginObject();
            while (reader.hasNext()) {
                String key = reader.nextName();
                if ("flow_sl".equals(key)) {
                    flow.flow_sl = reader.nextString();
                } else if ("run_id".equals(key)) {
                    flow.run_id = reader.nextString();
                } else if ("prcs_id".equals(key)) {
                    flow.prcs_id = reader.nextString();
                } else if ("flow_prcs".equals(key)) {
                    flow.flow_prcs = reader.nextString();
                } else if ("flow_id".equals(key)) {
                    flow.flow_id = reader.nextString();
                } else if ("subject".equals(key)) {
                    flow.subject = reader.nextString();
                } else if ("prcs_time".equals(key)) {
                    flow.prcs_time = reader.nextString();
                }

            }
            reader.endObject();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return flow;
    }

    // 解析业务类
    public static Business analysis_business(JsonReader reader) {
        Business business = new Business();
        try {
            reader.beginObject();
            while (reader.hasNext()) {
                String key = reader.nextName();
                if ("dryxe".equals(key)) {
                    business.dryxe = reader.nextString();
                } else if ("xsts".equals(key)) {
                    business.xsts = reader.nextString();
                } else if ("rwydwcl".equals(key)) {
                    business.rwydwcl = reader.nextString();
                }

            }
            reader.endObject();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return business;
    }
    
    // 解析业务类
    public static VersionInfo analysis_version_infor(JsonReader reader) {
        VersionInfo infor = new VersionInfo();
        try {
            reader.beginObject();
            while (reader.hasNext()) {
                String key = reader.nextName();
                if ("ver_time".equals(key)) {
                	infor.ver_time = reader.nextString();
                } else if ("ver".equals(key)) {
                	infor.ver = reader.nextString();
                } else if ("ver_url".equals(key)) {
                	infor.ver_url = reader.nextString();
                }

            }
            reader.endObject();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return infor;
    }


    /**
     * 获取反射类中的属性和属性值
     * 输出打印格式："Modifier Type : Name = Value"
     * Note: 对于未初始化的指针类型的属性，将不输出结果
     */
    public static void get_Reflection_Field_Value(Object r) {

        Class temp = r.getClass();    // 获取Class类的对象的方法之一

        try {
            LogUtils.e("public 属性");
            Field[] fb = temp.getFields();
            for (int i = 0; i < fb.length; i++) {

                Class cl = fb[i].getType();    // 属性的类型

                int md = fb[i].getModifiers();    // 属性的修饰域

                Field f = temp.getField(fb[i].getName());    // 属性的值
                f.setAccessible(true);
                Object value = (Object) f.get(r);

                // 判断属性是否被初始化
                if (value == null) {
                    LogUtils.e(Modifier.toString(md) + " " + cl + " : " + fb[i].getName());
                } else {
                    LogUtils.e(Modifier.toString(md) + " " + cl + " : " + fb[i].getName() + " = " + value.toString());
                }
            }

            LogUtils.e("public & 非public 属性");
            Field[] fa = temp.getDeclaredFields();
            for (int i = 0; i < fa.length; i++) {

                Class cl = fa[i].getType();    // 属性的类型

                int md = fa[i].getModifiers();    // 属性的修饰域

                Field f = temp.getDeclaredField(fa[i].getName());    // 属性的值
                f.setAccessible(true);    // Very Important
                Object value = (Object) f.get(r);

                if (value == null) {
                    LogUtils.e(Modifier.toString(md) + " " + cl + " : " + fa[i].getName());
                } else {
                    LogUtils.e(Modifier.toString(md) + " " + cl + " : " + fa[i].getName() + " = " + value.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 传入JsonReader,自动赋值
     */

    private static Object setKeyValue(JsonReader reader, Object object) {
        Class c = object.getClass();    // 获取Class类的对象的方法之一
        try {
            /******** 獲得成員變量的名字和類型 start  **********/
            Field[] fields = c.getDeclaredFields(); //获取属性集合
            Field.setAccessible(fields, true); //在类外面要想获取私有属性的值必须设置

            ArrayList<String> names = new ArrayList<String>();//存储变量名
            ArrayList<String> types = new ArrayList<String>(); //存储变量类型

            for (int i = 0; i < fields.length; i++) { //设置数组的值
                Field field = fields[i];
                String type1 = field.getGenericType().toString(); //变量类型
                String name1 = field.getName(); //变量名
                names.add(name1);
                types.add(type1);
            }
            /******** 獲得成員變量的名字和類型 end  **********/


            /******** 判斷類型並且給Object賦值 start************/
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                String type1 = types.get(i);
            }
            /******** 判斷類型並且賦值 end************/

        } catch (Exception e) {
            e.printStackTrace();
        }

        return object;
    }


    private static void beginObject(JsonReader reader, Object variable, String type1) throws IOException {

        if (type1.contains("String")) {
            variable = reader.nextString();
        } else if (type1.contains("int")) {
            variable = reader.nextInt();
        } else if (type1.contains("double")) {
            variable = reader.nextDouble();
        } else if (type1.contains("float")) {
            variable = reader.nextDouble();
        } else if (type1.contains("boolean")) {
            variable = reader.nextBoolean();
        } else if (type1.contains("long")) {
            variable = reader.nextLong();
        } else if (type1.contains("short")) {
            variable = reader.nextInt();
        } else {
            variable = null;
        }

    }

    private static Object analysis_base(String json, Object baseObject) {

        Class c = baseObject.getClass();    // 获取Class类的对象的方法之一
        try {
            /******** 獲得成員變量的名字和類型 start  **********/
            Field[] fields = c.getDeclaredFields(); //获取属性集合
            Field.setAccessible(fields, true); //在类外面要想获取私有属性的值必须设置

            ArrayList<String> names = new ArrayList<String>();//存储变量名
            ArrayList<String> types = new ArrayList<String>(); //存储变量类型

            for (int i = 0; i < fields.length; i++) { //设置数组的值
                Field field = fields[i];
                String type = field.getGenericType().toString(); //变量类型
                String name = field.getName(); //变量名
                names.add(name);
                types.add(type);
            }
            /******** 獲得成員變量的名字和類型 end  **********/


//            /******** 判斷類型並且給baseObject賦值 start************/
//            for (int i = 0; i < fields.length; i++){
//                Field field = fields[i];
//                String type = types.get(i);
//                String name = names.get(i);
//                String newKey = name.substring(0, 1).toUpperCase() + name.replaceFirst("\\w", "");//首字母大写
//                String setterMethodName = "set" + newKey;//set方法
//
//                Method setter;//调用setter方法
//
//                //field是baseObject的
//                //1、判断是不是数组
//                String firstABC = json.substring(0,1);
//                if (firstABC.equals("[")){
//                    //数组
//                    JSONArray jsonArray = new JSONArray(json);
//                }else {
//                    //对象
//                    JSONObject jsonObject = new JSONObject(json);
//
//
//                }
//            }
//            /******** 判斷類型並且賦值 end************/


            SetMethodReflect setMethodReflect = new SetMethodReflect(baseObject);
            for (String name :
                    names) {
                JSONArray jsonArray = new JSONArray(json);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
